'use strict';

    var app = angular.module('app');
    app.controller('bmFloorUpdateController', ['$scope', '$rootScope', '$state', '$timeout', 'toaster','$modal',
        function($scope, $rootScope, $state, $timeout, toaster, $modal) {
        	
        	$scope.record = {
        		regionid : $state.params.regionId,
        		status : 1
        	};
        	
        	$scope.regionName = $state.params.regionName;
        	$scope.stateRegionId = $state.params.regionId;
        	$scope.stateRegionName = $state.params.regionName;
        	
        	if($state.includes('**.update')){
                $scope.title="修改楼层信息";
                $scope.reset = false;//修改不允许重置
                var id = $state.params.id;
                $scope.record.floorid = id;
                activate(id);
            }else if($state.includes('**.create')){
            	 $scope.title="新增楼层信息";
            	 $scope.reset = true;//新增时可以重置
            }
        	
        	validate();
        	//重置表单
			$scope.resetFrom = function(){
			   $scope.record ={
			   	regionid : $state.params.regionId,
        		status : 1
			   };
			}
        	$scope.submit= function(){
        		var m = $scope.record;
	            if(m){
                    $scope.isDisabled = true;
                    $.ajax({
        				type: 'POST',
        	           	dataType: 'json',
    				    contentType:'application/json;charset=UTF-8',
    				    url : '/dac/bm/floor/insertOrUpdate',
    				    data: angular.toJson($scope.record)
        			}).then(function(result){
                   if(result.httpCode ==200){//成功
                        toaster.clear('*');
                        toaster.pop('success', '', "保存成功");
                        $timeout(function(){
                            $state.go("main.bm.floor.list", $state.params);
                        },2000);
                    }else{
                        toaster.clear('*');
                        toaster.pop('error', '', result.msg);
                        $scope.isDisabled = false;
                    }
	              });
          		}
       		 }
        	
        	function validate(){
                jQuery('form').validate({
                	rules: {
            			floorName: {
                           required: true,
                           stringCheck:[],
                           maxLengthB:[20]
            			},
           			    buildingname: {
           				    required:true
            			}

                   },
                   messages: {
                	   floorName: {
                           required: '请填写楼层名称',
                           maxLengthB:"楼层名称长度过长"
                   	   },
                  	   buildingname: {
                  		required:'请填写楼栋'
               	   }
                },
                    submitHandler: function() {
                        $scope.submit();
                    }
                });
            }
        	
        	 function activate(id) {
     	        $scope.loading = true;
             	$.ajax({
                type: 'PUT',
       				  url : '/dac/bm/floor/detail',
       	        dataType: 'json',
       				  contentType:'application/json;charset=UTF-8',
       				  data: angular.toJson({'id': id})
     			    }).then(function(result) {
     				     $scope.loading = false;
     				     if(result.httpCode == 200) {
     					    $scope.record = result.data;
     				     } else {
         					toaster.clear('*');
                  toaster.pop('error', '', result.msg);
                  $scope.isDisabled = true;
     				     }
     				     $scope.$apply();
     				});
          }
        	 
        	$scope.showBuildingList = function() {
     		 	$scope.items = [];
                  var modalInstance = $modal.open({
                      templateUrl : 'bmBuildingList.html',//script标签中定义的id
                      controller : 'bmBuildingListCtrl',//modal对应的Controller
                      size: 'lg',
                      resolve : {
                     	 regionId : function() {  
                              return $scope.record.regionid;  
                          }
                      }
                  });
                  
                  modalInstance.opened.then(function() {// 模态窗口打开之后执行的函数  
                       
                  });  
                  modalInstance.result.then(function(result) { 
                  	
                	if((result[0] != null || result[0] != '')&&$scope.record.buildingid != result[0]){
                      	$scope.record.seatid = '';
                        $scope.record.seatname='';
                    }
                	  
                  	$scope.record.buildingid = result[0];
                  	$scope.record.buildingname=result[1];
                  
                  }, function(reason) {  
                      console.log(reason);  
                  }); 

              };
              
              $scope.showSeatList = function() {
//            	 if($scope.record.buildingid == null || $scope.record.buildingid == ''){
//          			alert('请选择楼栋');
//          			$('#floorname').focus();
//          			return;
//          		}
       		 	$scope.items = [];
                    var modalInstance = $modal.open({
                        templateUrl : 'bmSeatList.html',//script标签中定义的id
                        controller : 'bmSeatListCtrl',//modal对应的Controller
                        size: 'lg',
                        resolve : {
                       	 regionId : function() {  
                                return $scope.record.regionid;  
                            },
                         buildingId : function(){
                        	 	return $scope.record.buildingid;
                         	},
                        }
                    });
                    
                    modalInstance.opened.then(function() {// 模态窗口打开之后执行的函数  
                         
                    });  
                    modalInstance.result.then(function(result) { 
                    	
                    	$scope.record.seatid = result[0];
                    	$scope.record.seatname=result[1];
                    }, function(reason) {  
                        console.log(reason);  
                    }); 

                };
    }]);
    
    app.controller('bmBuildingListCtrl', function($scope, $rootScope, $modal,$modalInstance,regionId) {
        $scope.param = { };
        $scope.param.regionId = regionId;
		$scope.param.status = "1";
		$scope.search = function () {
	        $scope.loading = true;
	        if($scope.pageFlag != 1){//未点击翻页
			  	   $scope.param.pageNum=1;//将页码设置为1，即跳转回第一页
			  	}else{//点击翻页
			  	   $scope.pageFlag = 2;//将开关值设置为2
			  	}
			$.ajax({
				type: 'PUT',
	            dataType: 'json',
				contentType:'application/json;charset=UTF-8',
				url : '/dac/bm/building/list',
				data: angular.toJson($scope.param)
			}).then(function(result) {
		        $scope.loading = false;
				if (result.httpCode == 200) {
					$scope.pageInfo = result;
				} else {
					$scope.msg = result.msg;
				}
				$scope.$apply();
			});
		}
		
		$scope.search();
		
        $scope.select = function(id,name) {  
        	$scope.params = [];
        	$scope.params[0]=id;
        	$scope.params[1]=name;
            $modalInstance.close($scope.params);  
        };  
        $scope.cancel = function() {  
            $modalInstance.dismiss('cancel');  
        }; 
        
        $scope.clear = function(){
        	$scope.select('','');
        }
        
     // 翻页
      $scope.pagination = function (page) {
            $scope.param.pageNum=page;
            $scope.pageFlag = 1;//将开关值设置为1
            $scope.search();
      };
    });
   
    app.controller('bmSeatListCtrl', function($scope, $rootScope, $modal,$modalInstance,regionId,buildingId) {
        $scope.param = { };
        $scope.param.regionId = regionId;
        $scope.param.buildingId = buildingId;
		$scope.param.status = "1";
		$scope.search = function () {
	        $scope.loading = true;
	        if($scope.pageFlag != 1){//未点击翻页
			  	   $scope.param.pageNum=1;//将页码设置为1，即跳转回第一页
			  	}else{//点击翻页
			  	   $scope.pageFlag = 2;//将开关值设置为2
			  	}
			$.ajax({
				type: 'PUT',
	            dataType: 'json',
				contentType:'application/json;charset=UTF-8',
				url : '/dac/bm/seat/list',
				data: angular.toJson($scope.param)
			}).then(function(result) {
		        $scope.loading = false;
				if (result.httpCode == 200) {
					$scope.pageInfo = result;
				} else {
					$scope.msg = result.msg;
				}
				$scope.$apply();
			});
		}
		
		$scope.search();
		
        $scope.select = function(id,name) {  
        	$scope.params = [];
        	$scope.params[0]=id;
        	$scope.params[1]=name;
            $modalInstance.close($scope.params);  
        };  
        $scope.cancel = function() {  
            $modalInstance.dismiss('cancel');  
        }; 
        $scope.clear = function(){
        	$scope.select('','');
        }
     // 翻页
      $scope.pagination = function (page) {
            $scope.param.pageNum=page;
            $scope.pageFlag = 1;//将开关值设置为1
            $scope.search();
      };
    });